
<template>
  <div class="app-container">
    <div class="filter-container">
       <el-select
        v-model="zdAdvertisingPlanDTO.type"
        clearable
        placeholder="类型"
        style="width: 150px;"
        class="filter-item"
      >
        <el-option :value="0" label="全部">全部</el-option>
        <el-option :value="1" label="广告">广告</el-option>
        <el-option :value="2" label="保险">保险</el-option> 
        </el-select>
       <el-input
            v-model="zdAdvertisingPlanDTO.insuranceProductsName"
            placeholder="机构名称"
            style="width: 150px;"
            class="filter-item"
            @keyup.enter.native="handleFilter"
          />
      <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            搜索
          </el-button>
      <el-button
        v-waves
        class="filter-item"
        type="success"
        style="float:right"
        @click="update"
      >
        更新
      </el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 95%;margin:auto;margin-top:10px"
    >
      <el-table-column prop="insuranceProductsName" label="名称"> </el-table-column>
      <el-table-column prop="type" label="类型"> 
        <template slot-scope="{ row }">
          <el-tag v-if="row.type==1">广告计划</el-tag>
          <el-tag v-if="row.type==2">保险计划</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="planName" label="计划名称"> </el-table-column>
      <el-table-column prop="weight" label="权重">
         <template slot-scope="{ row }">
          <el-input v-model="row.weight" type="number"></el-input>
          </template>
      </el-table-column>
    </el-table>
     <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="zdAdvertisingPlanDTO.current"
      :limit.sync="zdAdvertisingPlanDTO.size"
      @pagination="getWeightList"
    />
  </div>
</template>

<script>
import { weightList, weightListUpdate } from '@/api/advertising';
import Pagination from "@/components/Pagination";
export default {
    components: { Pagination },
  data() {
    return {
      zdAdvertisingPlanDTO: {
        current:1,
        size:10,
        type:0,
        insuranceProductsName:''
      },
      total:"",
      weight:[],
      tableData:[]
    };
  },
  created() {
     this.getWeightList(this.zdAdvertisingPlanDTO)
  },
  methods: {
    async getWeightList() {
      const res = await weightList(this.zdAdvertisingPlanDTO);
      this.tableData = res.data.data.records;
      this.total=res.data.data.total
    },
    handleFilter(){
       this.zdAdvertisingPlanDTO.current = 1;
      this.getWeightList(this.zdAdvertisingPlanDTO);
    },
    update(){
      let a=[]
      this.tableData.forEach((item) => {
        let z = {
          id: item.id,
          weight: (item.weight)*1,
        };
        a.push(z);
      });
      weightListUpdate(a).then((res) => {
        if (res.data.code === 200) {
          this.$notify({
            title: "Success",
            message: "更新成功",
            type: "success",
            duration: 2000,
          });
          this.getWeightList(this.zdAdvertisingPlanDTO);
        } else {
          this.$notify({
            title: "fail",
            message: "更新失败",
            type: "fail",
            duration: 2000,
          });
        }
      });
    }
  },
};
</script>

<style lang="scss" scoped>
.right {
  display: flex;
  flex-direction: row-reverse;
  margin-right: 50px;
  margin-top: 30px;
}
</style>
